// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import se.krka.kahlua.vm.KahluaTable;
import zombie.Lua.LuaManager;
import zombie.characters.IsoGameCharacter;
import zombie.characters.skills.PerkFactory;
import zombie.core.Translator;
import zombie.inventory.InventoryItem;
import zombie.inventory.ItemContainer;
import zombie.inventory.types.DrainableComboItem;
import zombie.iso.objects.IsoThumpable;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.Item;
import zombie.util.StringUtils;

public final class MultiStageBuilding {
    public static final ArrayList<MultiStageBuilding.Stage> stages = new ArrayList();

    public static ArrayList<MultiStageBuilding.Stage> getStages(IsoGameCharacter chr, IsoObject itemClicked, boolean cheat) {
        ArrayList arrayList = new ArrayList();

        for (int _int = 0; _int < stages.size(); _int++) {
            MultiStageBuilding.Stage stage = (MultiStageBuilding.Stage)stages.get(_int);
            if (stage.canBeDone(chr, itemClicked, cheat) && !arrayList.contains(stage)) {
                arrayList.add(stage);
            }
        }

        return arrayList;
    }

    public static void addStage(MultiStageBuilding.Stage stage) {
        for (int _int = 0; _int < stages.size(); _int++) {
            if (((MultiStageBuilding.Stage)stages.get(_int)).ID.equals(stage.ID)) {
                return;
            }
        }

        stages.add(stage);
    }

    public class Stage {
        public String name;
        public ArrayList<String> previousStage = new ArrayList();
        public String recipeName;
        public String sprite;
        public String northSprite;
        public int timeNeeded;
        public int bonusHealth;
        public boolean bonusHealthSkill = true;
        public HashMap<String, Integer> xp = new HashMap();
        public HashMap<String, Integer> perks = new HashMap();
        public HashMap<String, Integer> items = new HashMap();
        public ArrayList<String> itemsToKeep = new ArrayList();
        public String knownRecipe;
        public String thumpSound = "ZombieThumpGeneric";
        public String wallType;
        public boolean canBePlastered;
        public String craftingSound;
        public String completionSound = "BuildWoodenStructureMedium";
        public String ID;
        public boolean canBarricade = false;

        public String getName() {
            return this.name;
        }

        public String getDisplayName() {
            return Translator.getMultiStageBuild(this.recipeName);
        }

        public String getSprite() {
            return this.sprite;
        }

        public String getNorthSprite() {
            return this.northSprite;
        }

        public String getThumpSound() {
            return this.thumpSound;
        }

        public String getRecipeName() {
            return this.recipeName;
        }

        public String getKnownRecipe() {
            return this.knownRecipe;
        }

        public int getTimeNeeded(IsoGameCharacter isoGameCharacter) {
            int _int = this.timeNeeded;

            for (Entry entry : this.xp.entrySet()) {
                _int -= isoGameCharacter.getPerkLevel(PerkFactory.Perks.FromString((String)entry.getKey())) * 10;
            }

            return _int;
        }

        public ArrayList<String> getItemsToKeep() {
            return this.itemsToKeep;
        }

        public ArrayList<String> getPreviousStages() {
            return this.previousStage;
        }

        public String getCraftingSound() {
            return this.craftingSound;
        }

        public KahluaTable getItemsLua() {
            KahluaTable kahluaTable = LuaManager.platform.newTable();

            for (Entry entry : this.items.entrySet()) {
                kahluaTable.rawset(entry.getKey(), ((Integer)entry.getValue()).toString());
            }

            return kahluaTable;
        }

        public KahluaTable getPerksLua() {
            KahluaTable kahluaTable = LuaManager.platform.newTable();

            for (Entry entry : this.perks.entrySet()) {
                kahluaTable.rawset(PerkFactory.Perks.FromString((String)entry.getKey()), ((Integer)entry.getValue()).toString());
            }

            return kahluaTable;
        }

        public void doStage(IsoGameCharacter isoGameCharacter, IsoThumpable isoThumpable0, boolean _boolean) {
            int int0 = isoThumpable0.getHealth();
            int int1 = isoThumpable0.getMaxHealth();
            String string0 = this.sprite;
            if (isoThumpable0.north) {
                string0 = this.northSprite;
            }

            IsoThumpable isoThumpable1 = new IsoThumpable(
                IsoWorld.instance.getCell(), isoThumpable0.square, string0, isoThumpable0.north, isoThumpable0.getTable()
            );
            isoThumpable1.setCanBePlastered(this.canBePlastered);
            if ("doorframe".equals(this.wallType)) {
                isoThumpable1.setIsDoorFrame(true);
                isoThumpable1.setCanPassThrough(true);
                isoThumpable1.setIsThumpable(isoThumpable0.isThumpable());
            }

            int int2 = this.bonusHealth;
            switch (zombie.SandboxOptions.instance.ConstructionBonusPoints.getValue()) {
                case 1:
                    int2 = (int)((double)int2 * 0.5);
                    break;
                case 2:
                    int2 = (int)((double)int2 * 0.7);
                case 3:
                default:
                    break;
                case 4:
                    int2 = (int)((double)int2 * 1.3);
                    break;
                case 5:
                    int2 = (int)((double)int2 * 1.5);
            }

            Iterator iterator = this.perks.keySet().iterator();
            byte _byte = 20;
            switch (zombie.SandboxOptions.instance.ConstructionBonusPoints.getValue()) {
                case 1:
                    _byte = 5;
                    break;
                case 2:
                    _byte = 10;
                case 3:
                default:
                    break;
                case 4:
                    _byte = 35;
                    break;
                case 5:
                    _byte = 60;
            }

            int int3 = 0;
            if (this.bonusHealthSkill) {
                while (iterator.hasNext()) {
                    String string1 = (String)iterator.next();
                    int3 += isoGameCharacter.getPerkLevel(PerkFactory.Perks.FromString(string1)) * _byte;
                }
            }

            isoThumpable1.setMaxHealth(int1 + int2 + int3);
            isoThumpable1.setHealth(int0 + int2 + int3);
            isoThumpable1.setName(this.name);
            isoThumpable1.setThumpSound(this.getThumpSound());
            isoThumpable1.setCanBarricade(this.canBarricade);
            isoThumpable1.setModData(isoThumpable0.getModData());
            if (this.wallType != null) {
                isoThumpable1.getModData().rawset("wallType", this.wallType);
            }

            if (_boolean) {
                ItemContainer itemContainer = isoGameCharacter.getInventory();

                for (String string2 : this.items.keySet()) {
                    int int4 = (Integer)this.items.get(string2);
                    Item item = ScriptManager.instance.getItem(string2);
                    if (item != null) {
                        if (item.getType() == Item.Type.Drainable) {
                            InventoryItem inventoryItem0 = itemContainer.getFirstRecurse(
                                inventoryItem -> inventoryItem.getFullType().equals(item.getFullName())
                                        && ((DrainableComboItem)inventoryItem).getDrainableUsesInt() >= int4
                            );
                            if (inventoryItem0 != null) {
                                for (int int5 = 0; int5 < int4; int5++) {
                                    inventoryItem0.Use();
                                }
                            }
                        } else {
                            for (int int6 = 0; int6 < int4; int6++) {
                                InventoryItem inventoryItem1 = itemContainer.getFirstTypeRecurse(string2);
                                if (inventoryItem1 != null) {
                                    inventoryItem1.Use();
                                }
                            }
                        }
                    }
                }
            }

            for (String string3 : this.xp.keySet()) {
                isoGameCharacter.getXp().AddXP(PerkFactory.Perks.FromString(string3), (float)((Integer)this.xp.get(string3)).intValue());
            }

            int int7 = isoThumpable0.getSquare().transmitRemoveItemFromSquare(isoThumpable0);
            isoThumpable1.getSquare().AddSpecialObject(isoThumpable1, int7);
            isoThumpable1.getSquare().RecalcAllWithNeighbours(true);
            isoThumpable1.transmitCompleteItemToServer();
            if (isoGameCharacter != null && !StringUtils.isNullOrWhitespace(this.completionSound)) {
                isoGameCharacter.playSound(this.completionSound);
            }
        }

        public boolean canBeDone(IsoGameCharacter isoGameCharacter, IsoObject isoObject, boolean var3) {
            ItemContainer itemContainer = isoGameCharacter.getInventory();
            boolean _boolean = false;

            for (int _int = 0; _int < this.previousStage.size(); _int++) {
                if (((String)this.previousStage.get(_int)).equalsIgnoreCase(isoObject.getName())) {
                    _boolean = true;
                    break;
                }
            }

            return _boolean;
        }

        public void Load(String string0, String[] string1) {
            this.recipeName = string0;

            for (int int0 = 0; int0 < string1.length; int0++) {
                if (!string1[int0].trim().isEmpty() && string1[int0].contains(":")) {
                    String[] string2 = string1[int0].split(":");
                    String string3 = string2[0].trim();
                    String string4 = string2[1].trim();
                    if (string3.equalsIgnoreCase("Name")) {
                        this.name = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("TimeNeeded")) {
                        this.timeNeeded = Integer.parseInt(string4.trim());
                    }

                    if (string3.equalsIgnoreCase("BonusHealth")) {
                        this.bonusHealth = Integer.parseInt(string4.trim());
                    }

                    if (string3.equalsIgnoreCase("Sprite")) {
                        this.sprite = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("NorthSprite")) {
                        this.northSprite = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("KnownRecipe")) {
                        this.knownRecipe = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("ThumpSound")) {
                        this.thumpSound = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("WallType")) {
                        this.wallType = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("CraftingSound")) {
                        this.craftingSound = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("CompletionSound")) {
                        this.completionSound = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("ID")) {
                        this.ID = string4.trim();
                    }

                    if (string3.equalsIgnoreCase("CanBePlastered")) {
                        this.canBePlastered = Boolean.parseBoolean(string4.trim());
                    }

                    if (string3.equalsIgnoreCase("BonusSkill")) {
                        this.bonusHealthSkill = Boolean.parseBoolean(string4.trim());
                    }

                    if (string3.equalsIgnoreCase("CanBarricade")) {
                        this.canBarricade = Boolean.parseBoolean(string4.trim());
                    }

                    if (string3.equalsIgnoreCase("XP")) {
                        String[] string5 = string4.split(";");

                        for (int int1 = 0; int1 < string5.length; int1++) {
                            String[] string6 = string5[int1].split("=");
                            this.xp.put(string6[0], Integer.parseInt(string6[1]));
                        }
                    }

                    if (string3.equalsIgnoreCase("PreviousStage")) {
                        String[] string7 = string4.split(";");

                        for (int int2 = 0; int2 < string7.length; int2++) {
                            this.previousStage.add(string7[int2]);
                        }
                    }

                    if (string3.equalsIgnoreCase("SkillRequired")) {
                        String[] string8 = string4.split(";");

                        for (int int3 = 0; int3 < string8.length; int3++) {
                            String[] string9 = string8[int3].split("=");
                            this.perks.put(string9[0], Integer.parseInt(string9[1]));
                        }
                    }

                    if (string3.equalsIgnoreCase("ItemsRequired")) {
                        String[] string10 = string4.split(";");

                        for (int int4 = 0; int4 < string10.length; int4++) {
                            String[] string11 = string10[int4].split("=");
                            this.items.put(string11[0], Integer.parseInt(string11[1]));
                        }
                    }

                    if (string3.equalsIgnoreCase("ItemsToKeep")) {
                        String[] string12 = string4.split(";");

                        for (int int5 = 0; int5 < string12.length; int5++) {
                            this.itemsToKeep.add(string12[int5]);
                        }
                    }
                }
            }
        }
    }
}
